package edu.ucsc.gameAI;

import java.awt.Color;
import java.util.ArrayList;

import pacman.game.Constants.DM;
import pacman.game.Constants.GHOST;
import pacman.game.Constants.MOVE;
import pacman.game.Game;
import pacman.game.GameView;
import edu.ucsc.gameAI.Debug.MESSAGETYPE;
import edu.ucsc.gameAI.decisionTrees.binary.IBinaryNode;
import edu.ucsc.gameAI.fsm.IState;

public class RunAwayFromGhosts implements IAction, IBinaryNode {

	@Override
	public IAction makeDecision(Game game) {
		return null;
	}

	@Override
	public void doAction() {
		// TODO Auto-generated method stub

	}

	@Override
	public MOVE getMove() {
		// TODO Auto-generated method stub
		return null;
	}

	public MOVE getMove(Game game, IState state) {
				
		int pacmanIndex = game.getPacmanCurrentNodeIndex();
		int closestGhostIndex = 0;
		
		@SuppressWarnings("unused")
		GHOST closestGhost = null;
		double closestCurrentDistance = Double.MAX_VALUE;

		//find the closest ghost to pacman
		for (GHOST ghostType : GHOST.values()) {
			
			//skip this ghost if they are not a danger to him
			if(game.getGhostLairTime(ghostType) > 0 || game.isGhostEdible(ghostType))
				continue;
			
			int ghostIndex = game.getGhostCurrentNodeIndex(ghostType);
			if (closestCurrentDistance > game.getDistance(pacmanIndex, ghostIndex, DM.PATH)) {
				closestGhostIndex = ghostIndex;
				closestCurrentDistance = game.getDistance(pacmanIndex,ghostIndex, DM.PATH);
				closestGhost = ghostType;
			}
		}
		
		boolean pillStillSafe = true;
		
		if(state.getFarthestSafePellet() < 0 || !game.isPillStillAvailable(state.getFarthestSafePellet()))
		{
			pillStillSafe = false;
			state.setFarthestSafePellet(-1);
		}

		if(state.getFarthestSafePellet() != -1)
		{
			for (GHOST ghostType : GHOST.values()) 
			{
				int[] pacmanPathToFarthestPill = game.getShortestPath(pacmanIndex, state.getFarthestSafePellet());
	
				if(game.getGhostLairTime(ghostType) > 0 || game.isGhostEdible(ghostType))
					continue;
				
				for(int i = 0; i < pacmanPathToFarthestPill.length; i++)
				{
					if(pacmanPathToFarthestPill[i] == game.getGhostCurrentNodeIndex(ghostType) &&
							game.getShortestPathDistance(pacmanIndex, game.getGhostCurrentNodeIndex(ghostType)) < 
							PacmanConstants.safeDistance)
					{
						pillStillSafe = false;
						break;
					}
				}
				
				if(!pillStillSafe)
				{
					state.setFarthestSafePellet(-1);
					break;
				}
			}
		}
				
		int[] pills = game.getActivePillsIndices();
		ArrayList<Integer> activePills = new ArrayList<Integer>();
		for (int pill : pills)
			activePills.add(pill);
		
		boolean foundSafePill = false;
		
		int farthestPill;
		
		if (pillStillSafe)
			farthestPill = state.getFarthestSafePellet();
		else
			farthestPill = -1;
		
		while(!foundSafePill && state.getFarthestSafePellet() == -1)
		{
			foundSafePill = true;
			
		    pills  = new int[activePills.size()];
		    for (int i=0; i < pills.length; i++)
		    {
		        pills[i] = activePills.get(i).intValue();
		    }

			farthestPill = game.getFarthestNodeIndexFromNodeIndex(pacmanIndex, pills, DM.PATH);

			if (farthestPill == -1)
				break;
			
			int[] pacmanPathToFarthestPill = game.getShortestPath(pacmanIndex, farthestPill);
			
			for (GHOST ghostType : GHOST.values()) 
			{
				if(game.getGhostLairTime(ghostType) > 0 || game.isGhostEdible(ghostType))
					continue;
				
				for(int i = 0; i < pacmanPathToFarthestPill.length; i++)
				{
					if(pacmanPathToFarthestPill[i] == game.getGhostCurrentNodeIndex(ghostType) &&
							game.getShortestPathDistance(pacmanIndex, game.getGhostCurrentNodeIndex(ghostType)) < 
							PacmanConstants.safeDistance)
					{
						foundSafePill = false;
						break;
					}
				}
				
				if(!foundSafePill)
				{
					activePills.remove(new Integer(farthestPill));
					break;
				}
			}
		}
		
		MOVE nextMove;
		//Debug.debug("WANT TO RUN AWAY FROM GHOSTS--specifically fleeing from " + closestGhost.name(), Debug.MESSAGETYPE.ACTION);
		//int nextMoveIndex = FindJunction().findClosestJunction(game, game.getNeighbouringNodes(pacmanIndex, game.getPacmanLastMoveMade()), pacmanIndex, 6, pacmanIndex, path);
		
		if(farthestPill != -1)
		{
			int[] powerPillPath = game.getShortestPath(pacmanIndex, farthestPill);
			GameView.addPoints(game, Color.BLUE, powerPillPath );
			GameView.addPoints(game, Color.RED, farthestPill);
			state.setFarthestSafePellet(farthestPill);
			nextMove = game.getNextMoveTowardsTarget(pacmanIndex, farthestPill, DM.PATH);
		}
		else
		{
			Debug.debug("Couldn't find a safe pill!", MESSAGETYPE.STATEMENT);
			if(!state.runningFromGhostSet())
				state.setGhostRunningFrom(closestGhost);
			nextMove = game.getNextMoveAwayFromTarget(pacmanIndex, game.getGhostCurrentNodeIndex(state.getGhostRunningFrom()), DM.PATH);
		}
		//game.getNextMoveAwayFromTarget(pacmanIndex, closestGhostIndex, DM.PATH); //game.getNextMoveTowardsTarget(pacmanIndex, path.get(0), DM.PATH);
		//else
			//Debug.debug("No good path found. FREAK OUT!!!", MESSAGETYPE.STATEMENT);
		
	
		return nextMove;
	}
	
	@Override
	public String toString(){
		return "RunAwayFromGhostsAction";
	}
}//end class
